<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 带UI的通讯录1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889#import &amp;quot;Vie">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS-ContactsUI(联系人)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-ContactsUI(联系人)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 带UI的通讯录1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889#import &amp;quot;Vie">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:54:03.240Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS-ContactsUI(联系人)">
<meta name="twitter:description" content="####1. 带UI的通讯录1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889#import &amp;quot;Vie">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-ContactsUI(联系人)/"/>





  <title>iOS-ContactsUI(联系人) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/iOS-Special-Function/iOS-ContactsUI(联系人)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS-ContactsUI(联系人)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 带UI的通讯录<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AddressBookUI/AddressBookUI.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()&lt;ABPeoplePickerNavigationControllerDelegate&gt;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line">// 触摸显示</span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    //1. 创建联系人选择控制器</span><br><span class="line">    ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];</span><br><span class="line">    </span><br><span class="line">    //2. 设置代理 --&gt; 注意不是 Delegate</span><br><span class="line">    picker.peoplePickerDelegate = self;</span><br><span class="line">    </span><br><span class="line">    //3. 模态视图弹出</span><br><span class="line">    [self presentViewController:picker animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 选中某个联系人时调用</span><br><span class="line">/**</span><br><span class="line"> 以下两个方法,如果同时实现, 只会运行didSelectPerson: 方法</span><br><span class="line"> 两个方法, 应该实现一个即可</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person</span><br><span class="line">&#123;</span><br><span class="line">    //需求: 获取姓名 和 电话</span><br><span class="line">    </span><br><span class="line">    //1. 获取姓名</span><br><span class="line">    //Record --&gt; 就相当于一条记录</span><br><span class="line">    //lastName: 姓</span><br><span class="line">    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);</span><br><span class="line">    NSString *lastNameStr = (__bridge NSString *)(lastName);</span><br><span class="line">    NSLog(@&quot;lastNameStr: %@&quot;,lastNameStr);</span><br><span class="line">    </span><br><span class="line">    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);</span><br><span class="line">    NSString *firstNameStr = (__bridge NSString *)(firstName);</span><br><span class="line">    NSLog(@&quot;firstNameStr: %@&quot;,firstNameStr);</span><br><span class="line">    </span><br><span class="line">    CFRelease(lastName);</span><br><span class="line">    CFRelease(firstName);</span><br><span class="line">    </span><br><span class="line">    //2. 获取电话 --&gt; 电话是多数据类型</span><br><span class="line">    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);</span><br><span class="line">    </span><br><span class="line">    //2.1 获取电话的个数</span><br><span class="line">    CFIndex count = ABMultiValueGetCount(phones);</span><br><span class="line">    </span><br><span class="line">    //2.2 遍历联系人</span><br><span class="line">    for (CFIndex i = 0 ; i &lt; count; i++) &#123;</span><br><span class="line">        //2.3 获取联系电话的标签</span><br><span class="line">        NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones,  i);</span><br><span class="line">        NSLog(@&quot;label: %@&quot;,label);</span><br><span class="line">        </span><br><span class="line">        //2.4 获取联系电话</span><br><span class="line">        NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones,  i));</span><br><span class="line">        NSLog(@&quot;value: %@&quot;,value);</span><br><span class="line">    &#125;</span><br><span class="line">     </span><br><span class="line">    //3. 释放 CF 对象</span><br><span class="line">    CFRelease(phones);</span><br><span class="line"></span><br><span class="line">    //Core Foundation 和 Fundation 桥接的问题 --&gt; 面试题</span><br><span class="line">    //1. 有几种方式 2. 注意事项 注意释放 CF 对象</span><br><span class="line">    //将 CF 对象给 Foundatian</span><br><span class="line">   </span><br><span class="line">    //1. (__bridge type)&lt;#expression#&gt;) : 只是让 Foundation 框架暂时使用 CF 框架对象</span><br><span class="line">    //2. (__bridge_transfer )) / CFBridgingRelease : CF框架移交对象的管理权给 Foundation 框架</span><br><span class="line">    //将 Foundatian给 CF 对象 不常用</span><br><span class="line">    //3. (__bridge_retained &lt;#CF type#&gt;)&lt;#expression#&gt;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 选中联系人的某个属性的时候调用</span><br><span class="line"></span><br><span class="line">//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier</span><br><span class="line">//&#123;</span><br><span class="line">//    NSLog(@&quot;%d&quot;,property);</span><br><span class="line">//&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 取消选中联系人时调用</span><br><span class="line">// 在 iOS7下, 必须实现此方法, 否则就会崩溃</span><br><span class="line">//- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker</span><br><span class="line">//&#123;</span><br><span class="line">//    NSLog(@&quot;%s&quot;,__func__);</span><br><span class="line">//&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>####2. 不带UI的通讯录<br>1). AppDelegate授权<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions &#123;</span><br><span class="line">    //通讯录授权 --&gt; iOS6开始, 必须授权才能上架</span><br><span class="line">    /**</span><br><span class="line">     kABAuthorizationStatusNotDetermined = 0   用户未选择</span><br><span class="line">     kABAuthorizationStatusRestricted,         一些许可配置阻止了通讯录的交互</span><br><span class="line">     kABAuthorizationStatusDenied,             用户拒绝</span><br><span class="line">     kABAuthorizationStatusAuthorized          用户授权</span><br><span class="line">     */</span><br><span class="line"></span><br><span class="line">    //1. 获取授权状态</span><br><span class="line">    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();</span><br><span class="line">    </span><br><span class="line">    //2. 创建 AddrssBook</span><br><span class="line">    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);</span><br><span class="line">    </span><br><span class="line">    //3. 没有授权时就授权</span><br><span class="line">    if (status == kABAuthorizationStatusNotDetermined) &#123;</span><br><span class="line">        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) &#123;</span><br><span class="line">            //3.1 判断出错</span><br><span class="line">            if (error) &#123;</span><br><span class="line">                return;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            //3.2 判断是否授权</span><br><span class="line">            if (granted) &#123;</span><br><span class="line">                NSLog(@&quot;已经授权&quot;);</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                NSLog(@&quot;没有授权&quot;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //4. 释放 CF 对象</span><br><span class="line">    CFRelease(addressBook);</span><br><span class="line">    return YES;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>2). 代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;AddressBook/AddressBook.h&gt;</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line">// 触摸显示</span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event</span><br><span class="line">&#123;</span><br><span class="line">    // 点击屏幕获取所有联系人信息</span><br><span class="line">    </span><br><span class="line">    //1. 判断是否授权成功, 授权成功才能获取数据</span><br><span class="line">    if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) &#123;</span><br><span class="line">     </span><br><span class="line">        //2. 创建通讯录</span><br><span class="line">        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);</span><br><span class="line">        </span><br><span class="line">        //3. 获取联系人  --&gt; Array</span><br><span class="line">        CFArrayRef peosons = ABAddressBookCopyArrayOfAllPeople(addressBook);</span><br><span class="line">        </span><br><span class="line">        //4. 遍历联系人来获取数据(姓名和电话)</span><br><span class="line">        CFIndex count = CFArrayGetCount(peosons);</span><br><span class="line">        </span><br><span class="line">        for (CFIndex i = 0 ; i &lt; count; i++) &#123;</span><br><span class="line">            </span><br><span class="line">            //5. 获取单个联系人</span><br><span class="line">            ABRecordRef person = CFArrayGetValueAtIndex(peosons, i);</span><br><span class="line">            </span><br><span class="line">            //6. 获取姓名</span><br><span class="line">            NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));</span><br><span class="line">            NSString *firstName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));</span><br><span class="line">            NSLog(@&quot;lastName: %@, firstName: %@&quot;, lastName, firstName);</span><br><span class="line">            </span><br><span class="line">            //7. 获取电话</span><br><span class="line">            ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);</span><br><span class="line">            </span><br><span class="line">            //7.1 获取 count</span><br><span class="line">            CFIndex phoneCount = ABMultiValueGetCount(phones);</span><br><span class="line">            </span><br><span class="line">            //7.2 遍历ABMultiValueRef</span><br><span class="line">            for (CFIndex i = 0; i &lt; phoneCount; i++) &#123;</span><br><span class="line">                NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));</span><br><span class="line">                NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));</span><br><span class="line">                NSLog(@&quot;label: %@, value: %@&quot;,label, value);</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            NSLog(@&quot;\n\n&quot;);</span><br><span class="line">            </span><br><span class="line">            //8.1 释放 CF 对象</span><br><span class="line">            CFRelease(phones);</span><br><span class="line">        &#125;       </span><br><span class="line">        //8.1 释放 CF 对象</span><br><span class="line">        CFRelease(peosons);</span><br><span class="line">        CFRelease(addressBook);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<p>####3. iOS9 通讯录(新增)<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;ContactsUI/ContactsUI.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController () &lt;CNContactPickerDelegate&gt;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event &#123;</span><br><span class="line">    // 1. 创建控制器</span><br><span class="line">    CNContactPickerViewController *contact = [CNContactPickerViewController new];</span><br><span class="line">    // 2. 设置代理</span><br><span class="line">    contact.delegate = self;</span><br><span class="line">    // 3. 模态弹出</span><br><span class="line">    [self presentViewController:contact animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 选择联系人的时候调用</span><br><span class="line">- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact &#123;</span><br><span class="line">    // 1. 获取姓名</span><br><span class="line">    NSLog(@&quot;givenName: %@, familyName: %@&quot;, contact.givenName, contact.familyName);</span><br><span class="line">    // 2. 获取电话</span><br><span class="line">    for (CNLabeledValue *labeledValue in contact.phoneNumbers) &#123;</span><br><span class="line">        CNPhoneNumber *phoneNumber = labeledValue.value;</span><br><span class="line">        NSLog(@&quot;phoneNumber: %@&quot;, phoneNumber.stringValue);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 实现了此方法就可以选择多个联系人</span><br><span class="line">- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray&lt;CNContact *&gt; *)contacts &#123;</span><br><span class="line">    for (CNContact *contact in contacts) &#123;</span><br><span class="line">        </span><br><span class="line">        NSLog(@&quot;givenName: %@, familyName: %@&quot;, contact.givenName, contact.familyName);</span><br><span class="line">        for (CNLabeledValue *labeledValue in contact.phoneNumbers) &#123;</span><br><span class="line">            NSLog(@&quot;label: %@&quot;, labeledValue.label);</span><br><span class="line">            CNPhoneNumber *phoneNumber = labeledValue.value;</span><br><span class="line">            NSLog(@&quot;phoneNumber: %@&quot;, phoneNumber.stringValue);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-Bluetooth(蓝牙)/" rel="next" title="iOS-Bluetooth(蓝牙)">
                <i class="fa fa-chevron-left"></i> iOS-Bluetooth(蓝牙)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-CoreData(数据库)/" rel="prev" title="iOS-CoreData(数据库)">
                iOS-CoreData(数据库) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
